<?php

########################################
# Ajax Sentences
# @author: wdammak
# Project: Babili
########################################

session_start();

if(!isset($_SESSION)) die("you want logging");

require_once "configure.php";

	if (!function_exists('json_encode'))
	{
	  function json_encode($a=false)
	  {
		if (is_null($a)) return 'null';
		if ($a === false) return 'false';
		if ($a === true) return 'true';
		if (is_scalar($a))
		{
		  if (is_float($a))
		  {
			// Always use "." for floats.
			return floatval(str_replace(",", ".", strval($a)));
		  }
	
		  if (is_string($a))
		  {
			static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
			return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
		  }
		  else
			return $a;
		}
		$isList = true;
		for ($i = 0, reset($a); $i < count($a); $i++, next($a))
		{
		  if (key($a) !== $i)
		  {
			$isList = false;
			break;
		  }
		}
		$result = array();
		if ($isList)
		{
		  foreach ($a as $v) $result[] = json_encode($v);
		  return '[' . join(',', $result) . ']';
		}
		else
		{
		  foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
		  return '{' . join(',', $result) . '}';
		}
	  }
	}
		
	//Connect database
	$connect = mysql_connect(DB_SERVEUR,DB_USER,DB_PASS);
	mysql_select_db(DB_BDD) or die("erreur de connexion a la base de donnees");
		
	//Query Translated
	$selectCountTranslated="SELECT t.id_lang, count(t.id_key) as nbr
							FROM translation t							
							WHERE t.id_user_trans=".$_SESSION['babili']['id_user']->id_user." 
							GROUP BY t.id_lang";
	
	//Query Validated
	$selectCountValidated="SELECT t.id_lang, count(t.id_key) as nbr 
							FROM translation t
							WHERE t.id_user_validate=".$_SESSION['babili']['id_user']->id_user." 
							GROUP BY t.id_lang";
							
	//Get list of Languages
	$selectLangues="SELECT id_lang, short_name 
							FROM languages  
							ORDER BY id_lang
							";
	
	$resultLangues= mysql_query($selectLangues) or die(mysql_error());
	$resultTranslated= mysql_query($selectCountTranslated) or die(mysql_error());
	$resultValidated= mysql_query($selectCountValidated) or die(mysql_error());
	
	//Optimisation => without JOIN!
	$arr_langues=array();
	while ($item = mysql_fetch_assoc($resultLangues)){
		$arr_langues[$item['id_lang']]=$item['short_name'];
	}
	
	//OutputHtml
	function outputHtml($arr){
		global $arr_langues;
		while ($item = mysql_fetch_assoc($arr)) {
			/*($i++==0)?  $str_sent.=$arr_langues[$item["id_lang"]].':'.$item["nbr"] : 
						$str_sent.=",&nbsp;".$arr_langues[$item["id_lang"]].':'.$item["nbr"];*/
						
			$str_sent.='<option class="'.$arr_langues[$item["id_lang"]].'">'.$item["nbr"]."</option>";
		}
	
		/*if($str_sent!=''){
			$str_sent=explode(',',$str_sent);
			$i=0;
			foreach($str_sent as $item){
				if($i%8==0 && $i!=0)$str_sent1.="<br />".$item.',';
				else $str_sent1.=$item.",";
				$i++;
			}
			$str_sent=$str_sent1;
		}*/
		//return (substr($str_sent,-1)==',')?substr($str_sent,0,-1):$str_sent;
		
		return $str_sent;
		
	}

	$var1="new Array('".outputHtml($resultTranslated)."');";		
	$var2="new Array('".outputHtml($resultValidated)."');";

	mysql_close();

	die("$var1~$var2");
	
?>